package it.asvp.asvp_portal.data.util;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.entity.domain.BaseEntity;

import java.io.Serializable;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

@SuppressWarnings("serial")
public class OnPersistEvent extends EmptyInterceptor {

	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		
		
		if(entity instanceof BaseEntity){
		
		     for (int i = 0; i < propertyNames.length; i++) {
			 
		          if ("createdOn".equals(propertyNames[i])) {
		        	    state[i] = DateUtils.getCurrentDate();
		          }
		          if ("lastUpdate".equals(propertyNames[i])) {
		        	state[i] = DateUtils.getCurrentDate();
		          }
		     }
		     
		     return true;
		 }
		 return false;
	}

}
